//
//  CompanyVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/1/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CompanyVC.h"
#import "Masonry.h"
#import "CMMesgViewController.h"
#import "peopleVC.h"

#import "Controller.h"
#import "zijinzhanghuVC.h"
#import "ZhiYuanGuanLiRootViewController.h"
#import "ModifyPayPasswordController.h"

#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1];

#define Color153  [UIColor colorWithRed:80.f/255.f green:80.f/255.f blue:80.f/255.f alpha:1]
@interface CompanyVC ()<UITableViewDelegate,UITableViewDataSource>
{
 UITableView *_tableview;
 NSArray *cellName;
 NSString *iphoneNum;
    
}
@end

@implementation CompanyVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"CompanyVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"CompanyVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden=YES;
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"公司管理";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-11);
    }];

     cellName=@[@"公司信息",@"职员管理",@"修改支付密码"];
    //定义列表
    
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, self.view.frame.size.width, 150) style:UITableViewStylePlain];
    
    _tableview.dataSource=self;
    _tableview.delegate=self;
    _tableview.backgroundColor=[UIColor whiteColor];
    _tableview.scrollEnabled=NO;
    [self.view addSubview:_tableview];

    
}

-(void)backLogin
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return cellName.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //指定cellIdentifier为自定义的cell
    static NSString *CellIdentifier = @"OrderListCell";
    
    //自定义cell类有图片
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
//        UIImageView *cellImageV=[[UIImageView alloc]initWithFrame:CGRectMake(15, 12.5, 25, 25)];
//        cellImageV.image=[UIImage imageNamed:cellName[indexPath.row]];
//        [cell addSubview:cellImageV];
        
        UILabel *namela=[[UILabel alloc]initWithFrame:CGRectMake(15, 12.5, 150, 25)];
        namela.text=cellName[indexPath.row];
        namela.font=[UIFont systemFontOfSize:15];
        [cell addSubview:namela];
        
        if (indexPath.row==0) {
            UILabel *la=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-80, 12.5, 50, 25)];
            la.text=self.RZstate;
            la.font=[UIFont systemFontOfSize:15];
            [cell addSubview:la];
        }
        
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger cellindex=indexPath.row;
    switch (cellindex) {
        case 0:
            
            [MobClick event:@"companyInformation"];
            
            //公司信息
            [self compayXinXi];
            break;
        case 1:
            
            [MobClick event:@"staffManagement"];
            
            //职员管理
            [self clerkGuanli];
            break;
        case 2:
            
            [MobClick event:@"modifyPaymentPassword"];
            
            //修改支付密码
            [self Modifymima];
            break;
        default:
            break;
    }
}
 //公司信息
-(void)compayXinXi
{
    CMMesgViewController*CMMesgtVC=[CMMesgViewController new];
    [self.navigationController pushViewController:CMMesgtVC animated:YES];

}
 //职员管理
-(void)clerkGuanli
{
//    peopleVC*peoVC=[peopleVC new];
//    peoVC.compayid=self.COMid;
//    [self.navigationController pushViewController:peoVC animated:YES];
    
    ZhiYuanGuanLiRootViewController *peoVC=[ZhiYuanGuanLiRootViewController new];
    peoVC.compayid=self.COMid;
    [self.navigationController pushViewController:peoVC animated:YES];

}
//修改支付密码
-(void)Modifymima
{

    //先判断是否开资金账户
    [self  isfoundkaitong];

}

-(void)isfoundkaitong
{
    
    //先判断是否开通资金账户
    [Controller requestWithURL:ISFOUND jsondic:nil finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            //            "is_bind" = 1;
            //            "is_found" = 1;
            //            "is_main" = 1;
            //            "main_mobile" = 0;
            //            "main_name" = "";
            
            
            //            "main_mobile":0,               只有当前账号绑定公司 并且不是主账号的时候 此字段才有手机号  默认为0
            //            "is_bind":1,                       1已绑定   0未绑定
            //            "is_main":0,                      1主账号   0子账号
            //            "main_name":"",
            //            "is_found":1                     1已开通   0未开通
            
            //是否开通
            NSString *is_found=[NSString  stringWithFormat:@"%@",data.data[@"is_found"]];
            NSInteger is_fd=is_found.integerValue;
            
            //是否是 主账号，子账号
            NSString *is_main=[NSString  stringWithFormat:@"%@",data.data[@"is_main"]];
            NSInteger is_ma=is_main.integerValue;
            //是否绑定
            NSString *is_bind=[NSString  stringWithFormat:@"%@",data.data[@"is_bind"]];
            NSInteger is_b=is_bind.integerValue;
            
            
            iphoneNum=data.data[@"main_mobile"];
            
            if (is_fd==1) {
                //已开通  资金账户（修改支付密码）
                ModifyPayPasswordController *ModifyPayPassword=[[ModifyPayPasswordController alloc]init];
                [self.navigationController pushViewController:ModifyPayPassword animated:YES];
                
            }else{
                //未开通
                //先判断是否开通     没开通的时候 判断是否有公司   然后再判断主子账号
                
                if (is_b==1) {
                    //绑定了公司  提示其开通资金账户
                    if (is_ma==1) {
                        //主账号
                        //初始化AlertView
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                        message:@"贵公司还没有开通资金账户！"
                                                                       delegate:self
                                                              cancelButtonTitle:@"开通资金账户"
                                                              otherButtonTitles:nil,nil];
                        alert.tag = 40;
                        [alert show];
                        
                    }else{
                        //子账号
                        //初始化AlertView
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                        message:[NSString stringWithFormat:@"贵公司还没有开通资金账户！\n请联系主账号，登录信用行网站或吆喝APP开通资金账户。\n主账号：%@  %@",data.data[@"main_name"],iphoneNum]
                                                                       delegate:self
                                                              cancelButtonTitle:@"取消"
                                                              otherButtonTitles:@"呼叫客服",@"呼叫主账号",nil];
                        alert.tag = 41;
                        [alert show];
                        
                    }
                    
                }
                
            }
            
        }else
        {
            ALERTSHOW(data.message);
            
            
        }
    }];
    
    
    
}


//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==40) {
        if (buttonIndex==0) {
            //开通资金账户
            zijinzhanghuVC*zijinVC=[[zijinzhanghuVC alloc]init];
            [self.navigationController pushViewController:zijinVC animated:YES];
            
        }
        
    }else if(alertView.tag==41){
        
        if (buttonIndex==1) {
            //呼叫客服
            
            //进入打电话页面
            UIWebView *webview = [[UIWebView alloc] init];
            [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:4008850054"]]];
            [self.view addSubview:webview];
 
        }else if(buttonIndex==2){
            //呼叫主账号
            //进入打电话页面
            UIWebView *webview = [[UIWebView alloc] init];
            NSString *str=[NSString stringWithFormat:@"tel:%@",iphoneNum];
            [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
            [self.view addSubview:webview];

        }if (buttonIndex==0) {
            //取消
        
        }
        
    }
        
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
